package leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class LeetCodeMain62 {
    public static ArrayList<int[]> list=new ArrayList<>();
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int x=in.nextInt();
        int y=in.nextInt();
        int[][] arr=new int[x][y];
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                arr[i][j]=in.nextInt();
            }
        }
        dfs(arr,0,0);
        for (int[] a:list) {
            System.out.printf("(%d,%d)\n",a[0],a[1]);
        }
    }
    public static void dfs(int[][] arr,int x,int y){
        if (x<0||y<0||x>=arr.length||y>=arr[0].length||arr[x][y]==1||arr[x][y]==2){
            return;
        }
        if (y==arr[0].length-1&&x==arr.length-1){
            int[] arrs={x,y};
            list.add(arrs);

        }else if (arr[x][y]==0){
            arr[x][y]=2;
            int[] arrs={x,y};
            list.add(arrs);
            dfs(arr, x+1, y);
            dfs(arr, x, y+1);
            list.remove(list.size()-1);
            arr[x][y]=0;
        }
    }
    public int getMost(int[][] board) {
        // write code here
        int length1=board.length;
        int length2=board[0].length;
        int[][] dp=new int[length1][length2];
        dp[0][0]=board[0][0];
        for (int i = 1; i < length1; i++) {
            dp[i][0]=dp[i-1][0]+board[1][0];
        }
        for (int i = 1; i < length2; i++) {
            dp[0][i]=dp[0][i-1]+board[0][i];
        }
        for (int i = 1; i < length1; i++) {
            for (int j = 1; j < length2; j++) {
                dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1])+board[i][j];
            }
        }
        return dp[length1-1][length2-1];
    }
    public int longestPalindromeSubseq(String s) {
        int length=s.length();
        int[][] dp=new int[length][length];
        for (int i = 0; i < length; i++) {
            dp[i][i]=1;
        }
        for (int i = length-1; i>=0; i--) {
            for (int j = i+1; j < length; j++) {
                if (s.charAt(i)==s.charAt(j)){
                    dp[i][j]=dp[i+1][j-1]+2;
                }else {
                    dp[i][j]=Math.max(dp[i+1][j],dp[i][j-1]);
                }
            }
        }
        return dp[length-1][length-1];
    }
}
